package com.extremelylargepackage;

import android.util.Log;

public class StopWatch 
{
	private boolean _isPaused;
	private long _startTime;
	private long _pausedTime;
	private long _lapTime;
	private int _frames;
	private long _timeSinceReport;
	
	public StopWatch()
	{
		_isPaused = false;
		_startTime = 0;
		_pausedTime = 0;
		_lapTime = 0;
		_frames = 0;
	}
	
	public void start()
	{
		_startTime = curTime();
	}
	
	public long elapsedTime()
	{
		return curTime() - _startTime;
	}
	
	public void pause()
	{
		_isPaused = true;
		_pausedTime = curTime();
	}
	
	public void resume()
	{
		if(_isPaused)
		{
			_isPaused = false;
			_lapTime = curTime();
			_startTime -= curTime() - _pausedTime;
		}
	}
	
	public boolean isPaused()
	{
		return _isPaused;
	}
	
	public void lap()
	{
		_lapTime = curTime();
		_frames++;
		if(_lapTime - _timeSinceReport >= 5000)
		{
			Log.d("FPS", "" + (_frames / 5) );
			_frames = 0;
			_timeSinceReport = _lapTime;
		}
	}
	
	public long timeSinceLastLap()
	{
		if(_lapTime == 0)
			return 0;
		return curTime() - _lapTime;
	}
	
	private long curTime()
	{
		return System.nanoTime() / 1000000;
	}
}
